// 精英头目敌机类
import { TimeManager } from './TimeManager.js';

export class EnemyElite {
    constructor(x, y) {
        this.x = x;
        this.y = y;
        this.width = 50;  // 比普通敌机更大
        this.height = 50;
        this.speed = 60;  // 移动稍慢但更强大
        
        // 生命值系统
        this.maxHp = 3;   // 需要3发子弹才能击毁
        this.hp = this.maxHp;
        
        // 射击系统
        this.lastShootTime = 0;
        this.shootCooldown = 2000; // 2秒射击一次
        
        // 视觉特效
        this.enginePulse = 0;
        this.shieldPulse = 0;
        this.rotationAngle = 0;
        this.damageFlash = 0; // 受伤闪烁效果
    }
    
    update(deltaTime) {
        // 基于时间的移动计算
        this.y += this.speed * (deltaTime / 1000);
        
        // 更新视觉特效
        this.enginePulse = TimeManager.updatePhase(this.enginePulse, deltaTime, 8);
        this.shieldPulse = TimeManager.updatePhase(this.shieldPulse, deltaTime, 4);
        this.rotationAngle = TimeManager.updatePhase(this.rotationAngle, deltaTime, 2);
        
        // 更新受伤闪烁
        if (this.damageFlash > 0) {
            this.damageFlash = TimeManager.updateTimer(this.damageFlash, deltaTime);
        }
    }
    
    // 受击处理
    takeDamage() {
        this.hp--;
        this.damageFlash = 300; // 300毫秒闪烁效果
        return this.hp <= 0; // 返回是否被击毁
    }
    
    // 检查是否可以射击
    canShoot() {
        const currentTime = Date.now();
        if (currentTime - this.lastShootTime >= this.shootCooldown) {
            this.lastShootTime = currentTime;
            return true;
        }
        return false;
    }
    
    render(ctx) {
        ctx.save();
        
        // 受伤闪烁效果
        if (this.damageFlash > 0 && Math.floor(Date.now() / 100) % 2 === 0) {
            ctx.globalAlpha = 0.5;
        }
        
        // 绘制护盾光环
        this.renderShieldEffect(ctx);
        
        // 绘制主体机身
        this.renderMainBody(ctx);
        
        // 绘制武器系统
        this.renderWeapons(ctx);
        
        // 绘制引擎光效
        this.renderEngines(ctx);
        
        // 绘制装饰细节
        this.renderDecorations(ctx);
        
        // 绘制生命值指示器
        this.renderHealthBar(ctx);
        
        ctx.restore();
    }
    
    renderShieldEffect(ctx) {
        // 护盾能量环
        const shieldPulse = 0.6 + Math.sin(this.shieldPulse) * 0.4;
        const shieldRadius = 35 + Math.sin(this.shieldPulse * 1.5) * 3;
        
        const gradient = ctx.createRadialGradient(
            this.x + this.width/2, this.y + this.height/2, shieldRadius * 0.8,
            this.x + this.width/2, this.y + this.height/2, shieldRadius
        );
        
        gradient.addColorStop(0, `rgba(255, 100, 100, 0)`);
        gradient.addColorStop(0.8, `rgba(255, 100, 100, ${shieldPulse * 0.3})`);
        gradient.addColorStop(1, `rgba(255, 100, 100, 0)`);
        
        ctx.fillStyle = gradient;
        ctx.beginPath();
        ctx.arc(this.x + this.width/2, this.y + this.height/2, shieldRadius, 0, Math.PI * 2);
        ctx.fill();
        
        // 护盾能量环
        ctx.strokeStyle = `rgba(255, 150, 150, ${shieldPulse * 0.8})`;
        ctx.lineWidth = 2;
        ctx.beginPath();
        ctx.arc(this.x + this.width/2, this.y + this.height/2, shieldRadius - 5, 0, Math.PI * 2);
        ctx.stroke();
    }
    
    renderMainBody(ctx) {
        // 主体阴影效果
        ctx.shadowBlur = 10;
        ctx.shadowColor = '#ff6666';
        
        // 主机身（流线型设计，敌机向下飞行）
        const bodyGradient = ctx.createLinearGradient(this.x, this.y, this.x + this.width, this.y);
        bodyGradient.addColorStop(0, '#990000');
        bodyGradient.addColorStop(0.3, '#ff4444');
        bodyGradient.addColorStop(0.7, '#ff4444');
        bodyGradient.addColorStop(1, '#990000');
        
        ctx.fillStyle = bodyGradient;
        
        // 流线型机身（头部在下，尾部在上）
        ctx.beginPath();
        ctx.moveTo(this.x + 25, this.y + 50); // 机头在下方
        ctx.quadraticCurveTo(this.x + 20, this.y + 42, this.x + 15, this.y + 30);
        ctx.lineTo(this.x + 10, this.y + 15);
        ctx.lineTo(this.x + 40, this.y + 15);
        ctx.quadraticCurveTo(this.x + 30, this.y + 42, this.x + 25, this.y + 50);
        ctx.fill();
        
        // 机身上部（尾部区域）
        ctx.fillStyle = '#cc2222';
        ctx.fillRect(this.x + 12, this.y + 5, 26, 10);
        
        // 机身尾部（最上方）
        const tailGradient = ctx.createLinearGradient(this.x, this.y, this.x + this.width, this.y);
        tailGradient.addColorStop(0, '#660000');
        tailGradient.addColorStop(0.5, '#ff4444');
        tailGradient.addColorStop(1, '#660000');
        
        ctx.fillStyle = tailGradient;
        ctx.fillRect(this.x + 18, this.y, 14, 5);
        
        ctx.shadowBlur = 0;
    }
    
    renderWeapons(ctx) {
        // 武器系统（三管炮，适应敌机方向）
        const weaponGradient = ctx.createLinearGradient(this.x, this.y + 25, this.x + this.width, this.y + 25);
        weaponGradient.addColorStop(0, '#444444');
        weaponGradient.addColorStop(0.5, '#888888');
        weaponGradient.addColorStop(1, '#444444');
        
        ctx.fillStyle = weaponGradient;
        
        // 左武器（朝下）
        ctx.beginPath();
        ctx.moveTo(this.x + 5, this.y + 25);
        ctx.lineTo(this.x + 15, this.y + 30);
        ctx.lineTo(this.x + 15, this.y + 20);
        ctx.lineTo(this.x + 8, this.y + 15);
        ctx.fill();
        
        // 右武器（朝下）
        ctx.beginPath();
        ctx.moveTo(this.x + 45, this.y + 25);
        ctx.lineTo(this.x + 35, this.y + 30);
        ctx.lineTo(this.x + 35, this.y + 20);
        ctx.lineTo(this.x + 42, this.y + 15);
        ctx.fill();
        
        // 中央主炮
        ctx.fillStyle = '#666666';
        ctx.fillRect(this.x + 22, this.y + 10, 6, 25);
        
        // 炮口发光效果（在机头位置）
        const pulse = 0.8 + Math.sin(this.enginePulse) * 0.2;
        ctx.fillStyle = `rgba(255, 200, 100, ${pulse})`;
        ctx.fillRect(this.x + 23, this.y + 35, 4, 3); // 中央炮口
        ctx.fillRect(this.x + 13, this.y + 28, 4, 3); // 左炮口
        ctx.fillRect(this.x + 33, this.y + 28, 4, 3); // 右炮口
    }
    
    renderEngines(ctx) {
        // 引擎推进器（在机身尾部，向上喷射）
        const pulse = 0.7 + Math.sin(this.enginePulse) * 0.3;
        const flameLength = 12 + Math.sin(this.enginePulse * 1.5) * 4;
        
        // 左引擎（向上喷射）
        const leftGradient = ctx.createLinearGradient(this.x + 15, this.y, this.x + 15, this.y - flameLength);
        leftGradient.addColorStop(0, `rgba(255, 255, 255, ${pulse})`);
        leftGradient.addColorStop(0.3, `rgba(255, 100, 100, ${pulse * 0.8})`);
        leftGradient.addColorStop(0.7, `rgba(255, 0, 0, ${pulse * 0.6})`);
        leftGradient.addColorStop(1, 'rgba(200, 0, 0, 0)');
        
        ctx.fillStyle = leftGradient;
        ctx.fillRect(this.x + 12, this.y - flameLength, 8, flameLength);
        
        // 右引擎（向上喷射）
        const rightGradient = ctx.createLinearGradient(this.x + 30, this.y, this.x + 30, this.y - flameLength);
        rightGradient.addColorStop(0, `rgba(255, 255, 255, ${pulse})`);
        rightGradient.addColorStop(0.3, `rgba(255, 100, 100, ${pulse * 0.8})`);
        rightGradient.addColorStop(0.7, `rgba(255, 0, 0, ${pulse * 0.6})`);
        rightGradient.addColorStop(1, 'rgba(200, 0, 0, 0)');
        
        ctx.fillStyle = rightGradient;
        ctx.fillRect(this.x + 30, this.y - flameLength, 8, flameLength);
        
        // 引擎核心（在机身尾部）
        ctx.fillStyle = '#ffffff';
        ctx.fillRect(this.x + 14, this.y, 4, 4);
        ctx.fillRect(this.x + 32, this.y, 4, 4);
    }
    
    renderDecorations(ctx) {
        // 装饰条纹和细节
        const pulse = 0.8 + Math.sin(this.enginePulse * 1.5) * 0.2;
        
        // 机身条纹（适应敌机方向）
        ctx.strokeStyle = `rgba(255, 255, 255, ${pulse})`;
        ctx.lineWidth = 1;
        
        ctx.beginPath();
        ctx.moveTo(this.x + 20, this.y + 10);
        ctx.lineTo(this.x + 20, this.y + 42);
        ctx.stroke();
        
        ctx.beginPath();
        ctx.moveTo(this.x + 30, this.y + 10);
        ctx.lineTo(this.x + 30, this.y + 42);
        ctx.stroke();
        
        // 能量指示器（在机身中部）
        ctx.fillStyle = `rgba(255, 100, 100, ${pulse})`;
        for (let i = 0; i < 4; i++) {
            ctx.fillRect(this.x + 23 + i * 2, this.y + 21, 1, 4);
        }
        
        // 旋转装饰元件
        ctx.save();
        ctx.translate(this.x + this.width/2, this.y + this.height/2);
        ctx.rotate(this.rotationAngle);
        
        ctx.strokeStyle = `rgba(255, 150, 150, ${pulse * 0.6})`;
        ctx.lineWidth = 2;
        ctx.beginPath();
        ctx.moveTo(-8, -8);
        ctx.lineTo(8, 8);
        ctx.moveTo(8, -8);
        ctx.lineTo(-8, 8);
        ctx.stroke();
        
        ctx.restore();
    }
    
    renderHealthBar(ctx) {
        // 生命值条
        const barWidth = this.width;
        const barHeight = 4;
        const barX = this.x;
        const barY = this.y - 8;
        
        // 背景
        ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
        ctx.fillRect(barX, barY, barWidth, barHeight);
        
        // 生命值
        const healthPercent = this.hp / this.maxHp;
        let healthColor = '#ff4444';
        if (healthPercent > 0.6) healthColor = '#44ff44';
        else if (healthPercent > 0.3) healthColor = '#ffff44';
        
        ctx.fillStyle = healthColor;
        ctx.fillRect(barX + 1, barY + 1, (barWidth - 2) * healthPercent, barHeight - 2);
        
        // 边框
        ctx.strokeStyle = '#ffffff';
        ctx.lineWidth = 1;
        ctx.strokeRect(barX, barY, barWidth, barHeight);
    }
}